package com.ktwlrj.securitycenter.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktwlrj.core.constant.ResultCode;
import com.ktwlrj.core.exception.ActionException;
import com.ktwlrj.core.util.KtwlStringUtils;
import com.ktwlrj.core.util.convert.ConvertUtil;
import com.ktwlrj.securitycenter.controller.args.SafeCredentialArgs;
import com.ktwlrj.securitycenter.controller.args.SafeCredentialInfoArgs;
import com.ktwlrj.securitycenter.dto.SafeCredentialDto;
import com.ktwlrj.securitycenter.dto.SafeCredentialInfoDto;
import com.ktwlrj.securitycenter.entity.SafeCredentialEntity;
import com.ktwlrj.securitycenter.entity.SafeCredentialInfoEntity;
import com.ktwlrj.securitycenter.mapper.SafeCredentialInfoMapper;
import com.ktwlrj.securitycenter.mapper.SafeCredentialMapper;
import com.ktwlrj.securitycenter.service.SafeCredentialInfoService;
import com.ktwlrj.securitycenter.service.SafeCredentialService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 要件表(SafeCredential)业务层实现
 *
 * @author code generation
 * @date 2020-02-24 18:08:13
 * @since JDK1.8
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SafeCredentialServiceImpl extends ServiceImpl<SafeCredentialMapper, SafeCredentialEntity>
		implements SafeCredentialService {
	private final SafeCredentialMapper mapper;
	private final SafeCredentialInfoService credentialInfoService;
	private final SafeCredentialInfoMapper credentialInfoMapper;

	@Override
	public void save(SafeCredentialArgs args) throws RuntimeException {
		SafeCredentialEntity entity = ConvertUtil.map(args, SafeCredentialEntity.class);
		boolean result = super.save(entity);
		if (!result) {
			throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
		}
		args.getCredentialInfoList().forEach(v -> {
			v.setCredentialId(entity.getCredentialId());
		});
		credentialInfoService.batchSave(args.getCredentialInfoList());
	}

	@Override
	public void batchSave(List<SafeCredentialArgs> args) throws RuntimeException {
		List<SafeCredentialInfoArgs> credentialInfoList = new ArrayList<>();
		for (SafeCredentialArgs item : args) {
			if (StringUtils.isBlank(item.getCredentialId())) {
				item.setCredentialId(KtwlStringUtils.getUuId());
			}
			// 赋要件分组id及业务id到要件明细集合
			item.getCredentialInfoList().forEach(v -> {
				v.setCredentialInfoId(null);
				v.setCredentialId(item.getCredentialId());
				v.setBusiId(item.getBusiId());
			});
			credentialInfoList.addAll(item.getCredentialInfoList());
		}
		List<SafeCredentialEntity> arrayList = ConvertUtil.listAtoListB(args, SafeCredentialEntity.class);
		// 物理删除业务id下的要件分组信息
		mapper.deleteByBusiId(arrayList.get(0).getBusiId());

		boolean result = super.saveOrUpdateBatch(arrayList);
		if (!result) {
			throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
		}
		credentialInfoService.batchSave(credentialInfoList);
	}

	@Override
	public List<SafeCredentialDto> selectListByModel(String busiId) throws RuntimeException {
		// 查询要件分类
		QueryWrapper<SafeCredentialEntity> credentialQueryWrapper = new QueryWrapper<>();
		credentialQueryWrapper.lambda().in(SafeCredentialEntity::getBusiId, busiId);
		List<SafeCredentialEntity> credentialEntityList = mapper.selectList(credentialQueryWrapper);
		List<SafeCredentialDto> credentialList = ConvertUtil.listAtoListB(credentialEntityList,
				SafeCredentialDto.class);
		List<SafeCredentialInfoDto> credentialInfoDtoList = credentialInfoService.selectListByModel(busiId);
		if (credentialInfoDtoList != null && credentialInfoDtoList.size() > 0) {
			for (SafeCredentialDto credential : credentialList) {
				Iterator<SafeCredentialInfoDto> iteratorB = credentialInfoDtoList.iterator();
				List<SafeCredentialInfoDto> itemCredentialInfoList = new ArrayList<>();
				while (iteratorB.hasNext()) {
					SafeCredentialInfoDto b = iteratorB.next();
					if (credential.getCredentialId().equals(b.getCredentialId())) {
						itemCredentialInfoList.add(b);
						iteratorB.remove();
					}
				}
				credential.setCredentialInfoList(itemCredentialInfoList);
			}
		}
		return credentialList;
	}

	@Override
	public void deleteByBusiId(String busiId) throws RuntimeException {
		// 删除要件分类
		QueryWrapper<SafeCredentialEntity> credentialQueryWrapper = new QueryWrapper<>();
		credentialQueryWrapper.lambda().in(SafeCredentialEntity::getBusiId, busiId);
		mapper.delete(credentialQueryWrapper);
		// 删除要件
		QueryWrapper<SafeCredentialInfoEntity> credentialInfoQueryWrapper = new QueryWrapper<>();
		credentialInfoQueryWrapper.lambda().in(SafeCredentialInfoEntity::getBusiId, busiId);
		credentialInfoMapper.delete(credentialInfoQueryWrapper);
	}
}